package cn.wawi.controller.sys;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import com.wawi.common.easyui.Tree;
import cn.wawi.controller.BaseController;
import cn.wawi.dao.sys.DepartmentDao;
import cn.wawi.entity.sys.Department;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/sys_department")
public class DepartmentController extends BaseController<Department>{
	
	@Resource
	protected DepartmentDao<Department> departmentDao;
	/**
	 * 获取所有部门树
	 * @return easyui-tree
	 */
	@ResponseBody
	@RequestMapping(value="/tree",method=RequestMethod.GET)
	public List<Tree> tree(){
		List<Department> list = departmentDao.findAll(null);
		List<Tree> tree=createTree(list,null);
		return tree;
	}
	/*
	 * 生成部门树结构
	 */
	public List<Tree> createTree(List<Department> list,Long pId){
		List<Tree> trees=new ArrayList<Tree>();
		for(Department d: list){
           if(pId==d.getParentId()){
        	   Tree tree=new Tree();
        	   tree.setId(d.getId());
        	   tree.setText(d.getName());
        	   tree.setChildren(createTree(list,d.getId()));
        	   trees.add(tree);
           }
		}
		return trees;
	}
	/**
	 *主页
	 */
	@RequestMapping(value="/main",method=RequestMethod.GET)
	public String main() {
		return "sys/Department/main";
	}
}
